package org.locker.service.impl;

import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.FeedbackBo;
import org.locker.domain.vo.FeedbackVo;
import org.locker.domain.Feedback;
import org.locker.mapper.FeedbackMapper;
import org.locker.service.IFeedbackService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 反馈(投诉建议)Service业务层处理
 *
 * @author winnie
 * @date 2023-10-14
 */
@RequiredArgsConstructor
@Service
public class FeedbackServiceImpl  extends ServiceImpl<FeedbackMapper,Feedback>  implements IFeedbackService {

    private final FeedbackMapper baseMapper;

    /**
     * 查询反馈(投诉建议)
     */
    @Override
    public FeedbackVo queryById(Long id){
        return baseMapper.getById(id);
    }

    /**
     * 查询反馈(投诉建议)列表
     */
    @Override
    public Page<FeedbackVo> queryPageList(FeedbackBo bo, PageQuery pageQuery) {
        return baseMapper.listByPage(pageQuery.build(), bo);
    }

    /**
     * 查询反馈(投诉建议)列表
     */
    @Override
    public List<FeedbackVo> queryList(FeedbackBo bo) {
        LambdaQueryWrapper<Feedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Feedback> buildQueryWrapper(FeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Feedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), Feedback::getType, bo.getType());
        lqw.eq(bo.getUserId() != null, Feedback::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), Feedback::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), Feedback::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Feedback::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Feedback::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增反馈(投诉建议)
     */
    @Override
    public Boolean insertByBo(FeedbackBo bo) {
        Feedback add = MapstructUtils.convert(bo, Feedback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改反馈(投诉建议)
     */
    @Override
    public Boolean updateByBo(FeedbackBo bo) {
        Feedback update = MapstructUtils.convert(bo, Feedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Feedback entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除反馈(投诉建议)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
